ICPC data

library(leaflet)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
world <- st_read("data/world.shp")
## Reading layer `world' from data source 
##   `/home/brayand/Storage/School/from-data-to-knowledge-interpretation-visualization-presentation-course/Homework 10/data/world.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 197 features and 63 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -55.8917 xmax: 180 ymax: 83.59961
## Geodetic CRS:  GCS_unknown
data <- read_csv("data/icpc-full.csv")
## Rows: 2562 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (10): Host, City, Venue, University, Country, Team, Contestant 1, Conte...
## dbl   (6): Year, Rank, Score, Total, Score Percentage, Penalty
## lgl   (4): Gold, Silver, Bronze, Honorable
## date  (1): Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
host_counts <- data %>%
  select(Year, Host) %>%
  distinct() %>%
  group_by(Host) %>%
  summarise(Count = n()) %>%
  ungroup()

world <- world %>%
  left_join(host_counts, by = c("name" = "Host"))

world$Count[is.na(world$Count)] <- 0

pal <- colorNumeric("YlOrRd", domain = world$Count)

leaflet(data = world) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~pal(Count),
    weight = 1,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 3,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = ~paste(name, ": ", Count, " times hosted"),
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    )
  ) %>%
  addLegend(
    pal = pal,
    values = ~Count,
    opacity = 0.7,
    title = "Times Hosted",
    position = "bottomright"
  )